package cn.geckos.ioc.factory
{
	import flexunit.framework.TestCase;
	import flexunit.framework.TestSuite;
	
	import cn.geckos.mock.Config;
	import cn.geckos.mock.StackMock;
	import cn.geckos.ioc.factory.support.EmbedXmlFactory;
	import cn.geckos.mock.PlainTest;
	
	public class FactoryTest extends TestCase
	{
		public function FactoryTest(methodName:String)
		{
			super(methodName);
		}
		
		public static function testsuite():TestSuite
		{
			var ts:TestSuite = new TestSuite();
			//ts.addTest(new FactoryTest("testGetBean"));
			//ts.addTest(new FactoryTest("testSingleton"));
			//ts.addTest(new FactoryTest("testProtoType"));
			//ts.addTest(new FactoryTest("testIndependentEquals"));
			//ts.addTest(new FactoryTest("testCapability"));
			ts.addTest(new FactoryTest("testBeanFactoryAware"));
			return ts;
		}
		
		public function testGetBean():void
		{
			var factory:EmbedXmlFactory = new EmbedXmlFactory(new Config());
			var m1:StackMock = factory.getBean("t1");
			var m2:StackMock = factory.getBean("t2");
			
			trace(m1.array);
			trace(m2.array);
			var a:Array = m1.array[1].array;
			trace(a);
			for(var i:int = 0;i < a.length;i++)
			{
				//trace(m1.array[i])
				//trace(a[i]);
				assertTrue(m2.array[i] == a[i]);
			}
			
		}
		
		public function testSingleton():void
		{
			var factory:EmbedXmlFactory = new EmbedXmlFactory(new Config());
			var m1:StackMock = factory.getBean("t1");
			var m2:StackMock = factory.getBean("t2");
			
			assertTrue(m1.array[1] == m2);
			trace("test: "+m1.array[1].array);
		}
		
		public function testProtoType():void
		{
			var factory:EmbedXmlFactory = new EmbedXmlFactory(new Config());
			var m1:StackMock = factory.getBean("t3");
			var m4:StackMock = factory.getBean("t3");
			//var m2:StackMock = factory.getBean("t4");
			//var m3:StackMock = factory.getBean("t5");
			
			assertFalse(m1 == m4);
		}
		
		public function testIndependentEquals():void
		{
			var factory:EmbedXmlFactory = new EmbedXmlFactory(new Config());
			var m1:StackMock = factory.getBean("t3");
			var m2:StackMock = factory.getBean("t4");
			var m3:StackMock = factory.getBean("t5");
			
			assertTrue(m2 == m1.array[1]);
			assertTrue(m3 == m1.array[2]);
		}
		
		public function testCapability():void
		{
			var factory:EmbedXmlFactory
			var d:Number = new Date().getTime()
			for(var i:int=0;i<100;i++)
			{
				factory = new EmbedXmlFactory(new Config());
			}
			trace(new Date().getTime() - d);
			d = new Date().getTime();
			var m1:StackMock = factory.getBean("t1");
			trace(new Date().getTime() - d);
		}
		
		public function testBeanFactoryAware():void
		{
			var factory:EmbedXmlFactory = new EmbedXmlFactory(new Config());
			var t1:StackMock = factory.getBean("t2");
			var t3:StackMock = factory.getBean("t3");
			
			assertTrue(t1.getFactory() == factory);
			assertTrue(t3.getFactory() == factory);
		}
	}
}